<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width:  100%;
            height: 100px;
            background-color: skyblue;

            /* 标准流 */
            position: static;
            top: 0;
            left: 0;
        }
        body{
            height: 3000px;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        //获取div盒子元素 PHP
        var box = document.getElementsByTagName("div")[0]
        /* 滚动条事件 */
        window.onscroll = function(){
            /* 兼容处理,卷出高度 */
            var stop = window.document.documentElement.scrollTop || document.body.scrollTop
            if(stop > 500){
                //将他改成固定定位
                box.setAttribute("style","position: fixed")
            }else{
                //将他改成static
                box.setAttribute("style","position: static")
            }
        }
    </script>
</body>
</html>